package com.example.module_repair.repair_orders

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.module_repair.R
import com.example.module_repair.databinding.ActivityCreatrRepairOrdersBinding
import com.example.module_repair.repair_orders.model.CraeateRepairOrderModel
import com.example.module_repair.repairnet.RepairViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
import kotlin.random.Random

/*
创建报修工单
 */
@Route(path = "/repair/CreatrRepairOrdersActivity")
@AndroidEntryPoint
class CreatrRepairOrdersActivity :
    BaseActivity<RepairViewModel, ActivityCreatrRepairOrdersBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_creatr_repair_orders

    val repairViewModel: RepairViewModel by viewModels()


    override fun initView() {
        super.initView()

    }

    override fun initData() {
        super.initData()
        mBinding.repairTopBar.backActivity.setOnClickListener {
            finish()
        }

        var s = MMKV.defaultMMKV().decodeInt("userId")
        mBinding.repairTopBar.repairSubmit.setOnClickListener {
            var orderNumberr = ""
            for (i in 0..9) {
                orderNumberr += Random.nextInt(9)
            }

            val craeateRepairOrderModel = CraeateRepairOrderModel(
                contactMethod = mBinding.repairShoujihao.text.toString().toLong(),
                faultDescription = "${mBinding.repairGuzhangmiaoshu.text}",
                faultPhotos = "https://picsum.photos/200/300",
                id = 1,
                orderNumber = "${"WV" + orderNumberr}",//不能相同且必须传
                processingStatus = "待处理",
                repairLocation = mBinding.location.text.toString().toInt(),
                repairName = mBinding.repairName.text.toString(),
                repairTime = SimpleDateFormat.getInstance()
                    .format(Date(System.currentTimeMillis())),
                userId = s  //userid  从mmkv中获取
            )


            repairViewModel.createRepairOrders(craeateRepairOrderModel)
            Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show()
            lifecycleScope.launch {
                delay(1000)
                finish()
            }


        }


        mBinding.selectedLocation.setOnClickListener {
            val popupMenu = PopupMenu(this, mBinding.selectedLocation) // `it` 是点击的 View（按钮）
            popupMenu.menuInflater.inflate(R.menu.popup_menu, popupMenu.menu)

            // 设置菜单项点击监听
            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.menu_item1 -> {
                        mBinding.location.setText("1")
                    }

                    R.id.menu_item2 -> {
                        mBinding.location.setText("2")
                    }
                    R.id.menu_item3 -> {
                        mBinding.location.setText("3")
                    }
                }
                true
            }
            popupMenu.show() // 显示菜单
        }



        //点击+号
        mBinding.selectedpicture.setOnClickListener {


        }


    }

    override fun initListener() {
        super.initListener()
    }


}